const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin'); 
const { CleanWebpackPlugin } = require('clean-webpack-plugin');  
const glob = require('glob');

 
 
const ASSET_PATH= process.env.NODE_ENV === 'production' ? '/ScanUnitySlame2018_4_7_1_/webpack/dist/' : '' 

//设置需要打包的文件
function entries(globPath) {
  
    var files = glob.sync(globPath);
    var entries = {}, entry, dirname, basename,ssss;

    for (var i = 0; i < files.length; i++) {
        entry = files[i];
        dirname = path.dirname(entry); 
        basename = path.basename(entry, '.js'); 
        if (dirname === "src/Build"||dirname === "src/slam") {
            continue;
        }  
        entries[(path.join(dirname, basename)).replace(/\\/g,'/')] = './' + entry;

    }  
    return entries;
}


defaultConfig = { 
    // entry: entries('src/**/*.js'), 
    entry: entries('./src/index.ts'), 

    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist'),
        clean: true,
        publicPath:ASSET_PATH , //公共路径
        assetModuleFilename: '[path]/[hash][ext][query]'//图片资源路径

    },
    module: {
        rules: [ 
            {
                test: /\.(png|svg|jpg|jpeg|gif)$/i,
                type: 'asset/resource',

            },
            {
                test: /\.s[ac]ss$/i,
                use: [
                  // 将 JS 字符串生成为 style 节点
                  'style-loader',
                  // 将 CSS 转化成 CommonJS 模块
                  'css-loader',
                  // 将 Sass 编译成 CSS
                  'sass-loader',
                ],
            },
            {
                test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/,
            },
        ],

    },
    plugins: [
        
        new HtmlWebpackPlugin({
            template: './src/index.html'
        }),
        
        new CleanWebpackPlugin(),
    ],

    devServer: {
        contentBase: path.join(__dirname, 'dist'),
        host: "localhost",
        port: 8080,
        open: true,
        compress: true,
        https: true, 
    },

    node: {
        global: false,
        __filename: false,
        __dirname: false,
    },

    stats: {
        assets: false,
        builtAt: false,
        modules: false,
        entrypoints: false
    },

    resolve: {
        extensions: [ '.tsx', '.ts', '.js' ],
      },

};

module.exports= defaultConfig;